package com.slm.report.view.data;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import androidx.core.content.ContextCompat;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.StringUtils;
import com.slm.report.R;
import com.slm.report.databinding.ViewTextContentBinding;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.view.data
 * @ClassName: TimeContentView
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/1 22:40
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/1 22:40
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class TimeContentView extends BaseContentView {
    private ViewTextContentBinding viewTextContentBinding = null;
    private TimePickerView pvTime;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public TimeContentView(Context context) {
        super(context);
        mContext = context;
        viewTextContentBinding = ViewTextContentBinding.inflate(LayoutInflater.from(getContext()), this, true);

    }


    @Override
    void upDataContent() {
        viewTextContentBinding.titleTv.setText(contentDataBean.getTitleStr());

        if (isEdit) {
            if (StringUtils.isEmpty(contentDataBean.getContent())) {
                viewTextContentBinding.contentTv.setText("请选择时间");
            } else {
                viewTextContentBinding.contentTv.setText(contentDataBean.getContent());
            }
            viewTextContentBinding.contentTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (pvTime == null) {
                        initTimePicker();
                    }
                    pvTime.show(v);
                }
            });


        } else {
            viewTextContentBinding.contentTv.setText(contentDataBean.getContent());
            viewTextContentBinding.contentTv.setOnClickListener(null);
        }
    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出
        pvTime = new TimePickerBuilder(mContext, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                viewTextContentBinding.contentTv.setText(getTime(date));
                contentDataBean.setContent(getTime(date));
                viewTextContentBinding.contentTv.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true)
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(6)
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
    }


    private String getTime(Date date) {//可根据需要自行截取数据显示
        return format.format(date);
    }
}
